<?php
/**
 * Created by Command.
 * User: Vito
 */

namespace app\admin\curd\pay_record;

use app\common\curd\Index;
use app\model\Housing;
use think\db\Query;
use think\facade\Db;
use app\model\Timing;
class PayRecordIndex extends Index
{
    /**
     * 关键词查询指定字段
     * @var string[]
     */
    protected $keywordQueryArgs = [];
    //追加标签数据
    protected $labelCallback = [
        'get_status_enum' => ['name' => '状态'],
    ];
    //查询条件
    protected $where = [];
    //字段
    protected $field = [];
    //排除字段
    protected $withoutField = false;
    //关联
    protected $with = [
        'room',
        // 'building',
        'children',
        // 'ceshi'
    ];
    protected $order = [
        'PayRecord.id' => 'desc',
        // 'Room.status' => 'desc'
    ];

    // protected $fetchSql = true;
    /**
     * 需要查询条件的字段
     * 数据库字段和参数字段名不一样 'db_field' => 'param_field'
     * 数据库字段和参数字段名一样 'field'
     * @var array
     */
    protected $queryField = [];
    protected $middleware = [
        'middleWhere'
    ];
    protected function middleWhere($next)
    {
        $room_id = $this->request['room_id'];
       
        if ($room_id) {
            $this->field =
                [
                    'Room.id' => 'room_id',
                    'PayRecord.*'
                ];
        }
        return $next();
    }

    protected function queryCallback(Query $query)
    {
        // halt($this->request);
        $room_id = $this->request['room_id'];

        $type = $this->request['type'];
        // print_r($room_id);
        // die;
        if ($room_id) {
            $query
                ->join('wuye_room Room', 'Room.id = PayRecord.room_id', 'right')
                ->where('Room.housing_id', '=',$room_id);
        }
        if ($type) {
            $Time = Timing::find(1);
            $day = $Time['time'];
            $newTime = strtotime("-$day day");
            $where[] = ['create_time','<', $newTime];
            $where[] = ['status','=',0];
            $query->where($where);
        }
    }
    // 时间戳转事件
    protected function formatMiddleware($next)
    {
        $response = $next();
        foreach ($response as $key => $value) {
            $response[$key]['fee_date'] = date("Y-m-d",$value['fee_date']);

            $children =  $response[$key]['children'];
            foreach ($children as $index => $item) {
                $children[$index]['fee_date'] = date("Y-m-d", $item['fee_date']);
            }
        }
        return $response;
    }
    
    
}